# -*- coding: utf-8 -*-
import scrapy
from ..items import ZhongyaowangItem


class ZywSpider(scrapy.Spider):
    name = 'zyw'
    start_urls = ['http://www.ecnol.com/article_cat-6.html']

    def parse(self, response):
        all_li = response.xpath("//ul[@class='helptree']/li[@class='tree3']")
        for li in all_li:
            fenlei = li.xpath("./a/text()")[0].extract()
            href_fl = 'http://www.ecnol.com/' + li.xpath("./a/@href")[0].extract()
            yield scrapy.Request(url=href_fl, callback=self.parse_wz, meta={'fenlei': fenlei})

    def parse_wz(self, response):
        fenlei = response.meta['fenlei']
        all_li = response.xpath("//ul[@class='wz_list']/li")
        for li in all_li:
            biaoti = li.xpath("./a/text()")[0].extract()
            shijian = li.xpath("./span/text()")[0].extract()
            href_wz = 'http://www.ecnol.com/' + li.xpath("./a/@href")[0].extract()
            yield scrapy.Request(url=href_wz, callback=self.parse_xq,
                                 meta={'fenlei': fenlei, 'biaoti': biaoti})

    def parse_xq(self, response):
        fenlei = response.meta['fenlei']
        biaoti = response.meta['biaoti']
        laiyuan = response.xpath("string(//div[@class='info'])")[0].extract().replace('\n', '').replace('\t',
                                                                                                        '').strip()
        neirong = response.xpath("string(//div[@class='txt1'])")[0].extract().replace('\n', '').replace('\t',
                                                                                                        '').strip()
        item = ZhongyaowangItem()
        item['fenlei'] = fenlei
        item['biaoti'] = biaoti
        item['neirong'] = neirong
        yield item
